//
//  SDLoginManager.m
//  SKinDiary
//
//  Created by pcyang on 16/7/11.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDLoginManager.h"
#import "SDAccountInfoManager.h"
#import "SDLoginData.h"

@interface SDLoginManager()<SDFormBaseDataDelegate>
{
    
}
@property (nonatomic,strong)SDLoginData*  loginData;
@property (nonatomic,strong)SDAccountInfoManager* accountInfo;

//@property (nonatomic,strong)VPLogoutData* logoutData;

//@property (nonatomic,strong)VPAccountInfoManager* accountInfo;
@end

@implementation SDLoginManager
static SDLoginManager *s_instance;

+(SDLoginManager*)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,
                  ^{
                      s_instance  = [[super allocWithZone:nil]  init];
                      
                      [[NSNotificationCenter defaultCenter] addObserver:s_instance selector:@selector(loginDidStartNotification:) name:kLoginDidStartNotificaion object:nil];
//                      [[NSNotificationCenter defaultCenter] addObserver:s_instance selector:@selector(loginDidFailNotification:) name:kLoginServiceeDidFailNotification object:nil];
//                      [[NSNotificationCenter defaultCenter] addObserver:s_instance selector:@selector(loginDidLogOutNotification:) name:kLoginServiceDidLogOutNotification object:nil];
                      [[NSNotificationCenter defaultCenter] addObserver:s_instance selector:@selector(loginDidSuccessNotification:) name:kLoginMgrDidSucceedNotificaion object:nil];
                      
                  });
    return s_instance ;
}


- (id)init
{
    self = [super init];
    if (self) {
        
        _accountInfo = [SDAccountInfoManager sharedInstance];
        if ((CHECK_VALID_STRING(_accountInfo.userId) && CHECK_VALID_STRING(_accountInfo.sessionId))) {
          //  self.loginState = SDLoginStateLoggedIn;
        }
    }
    return self;
}

-(BOOL)isLogin
{
    BOOL login = (CHECK_VALID_STRING(_accountInfo.userId) && CHECK_VALID_STRING(_accountInfo.sessionId));
    return login;
}

-(void)setLoginState:(SDLoginState)loginState
{
    // [self willChangeValueForKey:@"loginState"];
    _loginState = loginState;
    // [self didChangeValueForKey:@"loginState"];
    
    if (_loginState == SDLoginStateLoggedIn)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:kLoginMgrDidSucceedNotificaion object:nil userInfo:nil];
    }
}

-(NSString*)userId
{
    return [SDAccountInfoManager sharedInstance].userId;
}

-(NSString*)sessionId
{
    return [SDAccountInfoManager sharedInstance].sessionId;
}


- (void)showLoading{
    [self finishLoading];
    [VPMBProgressHUD showIndicatorWithText:@"登录中"];
}

- (void)finishLoading
{
    [VPMBProgressHUD hideIndicator];
}

- (void)failedLoading
{
    [self finishLoading];
    [VPMBProgressHUD showAttendAlert:@"登录失败" andIcon:nil];
}

-(void)doLoginWithUserName:(NSString*)userName password:(NSString*)password
{
    if (_loginData == nil) {
        _loginData = [[SDLoginData alloc] init];
        _loginData.delegate = self;
    }
    _loginData.mobileNumber = userName;
    _loginData.password = password;
    [_loginData uploadFormDataWithUserInfo:nil];
}

-(void)logOut
{
    if (![self isLogin])
        return;
    
    [[SDAccountInfoManager sharedInstance] clearAccount];
}

#pragma mark -
#pragma mark -  Login Notifications
- (void)loginDidStartNotification:(NSNotification *)not
{
}

- (void)loginDidSuccessNotification:(NSNotification *)not
{
    
}

- (void)loginDidFailNotification:(NSNotification *)not
{
    
}

- (void)loginDidLogOutNotification:(NSNotification *)not
{
    
}

#pragma mark -
#pragma mark -  SDFormBaseDataDelegate
-(void) willUploadOfFormData:(SDFormBaseData*) formdata
{
    if ([formdata isKindOfClass:[SDLoginData class]])
    {
        [self finishLoading];
        [self showLoading];
    }
    
}
-(void) formData:(SDFormBaseData*)formdata userInfo:(NSDictionary*) userInfo didFinishLoadWithError:(NSError*)error
{
    [self finishLoading];
    if (!error) {
        if ([formdata isKindOfClass:[SDLoginData class]]) {
            
            if (CHECK_VALID_STRING(_loginData.session.token) && CHECK_VALID_STRING(_loginData.userInfo.uid)) {
                
                _accountInfo.sessionId = [NSString stringWithString:_loginData.session.token];
                _accountInfo.userId = [NSString stringWithString:_loginData.userInfo.uid];
                _accountInfo.nickName = [NSString stringWithString:[_loginData.userInfo.phoneNo safeString]];
                
                _accountInfo.mobileNumber = _loginData.mobileNumber;
                _accountInfo.password = _loginData.password;
                
                [_accountInfo saveAccount];
                [self finishLoading];
                [self setLoginState:SDLoginStateLoggedIn];

            }
            else
            {
                [self failedLoading];
                
            }

        }
       
    }else{
        [self failedLoading];
    }
}

@end
